package com.bookm.bookm.service.impl;

import cn.hutool.core.util.PageUtil;
import cn.hutool.db.Db;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.handler.BeanHandler;
import cn.hutool.db.handler.BeanListHandler;
import cn.hutool.db.sql.Condition;
import cn.hutool.db.sql.ConditionBuilder;
import cn.hutool.db.sql.SqlBuilder;
import com.bookm.bookm.entity.Book;
import com.bookm.bookm.entity.MyPage;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.BookService;
import com.bookm.bookm.utils.ResultUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 萧然
 */
public class BookServiceImpl implements BookService {
    @Override
    public Book getBook(String bookId){
        Book book = null;
        try {
            // 使用Entity类的create方法构造sql语句，并指定where条件，使用BeanHandler指定转换的类对象，Db类就能将查询到的Entity数据转换为我们指定的类对象数据
            book = Db.use().find(Entity.create("book").set("book_id", bookId), BeanHandler.create(Book.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    @Override
        public Result getBookList(Integer curPage, Integer pageSize, Entity entity, Integer cid, String searText) {
        List<Book> books = null;
        try {
            // 判断 图书类别 是否大于0
            if(cid > 0){
                // 添加 where 条件
                entity.set("class_", cid);
            }
            // 如果搜索的文本不为空，则需要在 sql 语句中构造添加 where 条件
            if(!"".equals(searText)){
                String cq = "";
                if(cid > 0){
                    cq = "class_="+cid+" and ";
                }
                // 最后执行的 sql 语句
                String sql = "select * from book where "+cq+"(book_id like ? or book_name like ? or author like ?)";
                // 执行查询并将结果集转换成 List<Book>
                books = Db.use().page(sql,new Page(curPage-1, pageSize), BeanListHandler.create(Book.class), "%"+searText+"%", "%"+searText+"%", "%"+searText+"%");
            } else {
                books = Db.use().page(entity,new Page(curPage-1, pageSize), BeanListHandler.create(Book.class));
            }
            System.out.println(books);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        List<Object> rst = new ArrayList<>();
        rst.add(books);
        // 获取查询到的记录数
        int total = Math.toIntExact(count(cid, searText));
        // 分页的总页数计算
        int totalPage = PageUtil.totalPage((total), pageSize);
        rst.add(new MyPage(curPage, pageSize, total, totalPage));
        return ResultUtil.success(rst);
    }

    @Override
    public long count(Integer cid, String searText) {
        long rst = 0;
        // 基础的 sql 语句
        String sql = "select count(*) as num from book where ";
        // 判断搜索文本是否不为空
        if(!"".equals(searText)){
            // 图书类别是否传递，传递则拼接 where 条件
            if(cid > 0){
                sql+="class_="+cid+" and ";
            }
            sql+="(book_id like ? or book_name  like ? or author like ?)";
            try {
                // 执行查询并获取到记录数
                rst = Db.use().query(sql, "%"+searText+"%", "%"+searText+"%", "%"+searText+"%").get(0).getInt("num");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            // 此处为当搜索文本不存在时的判断
            try {
                if(cid > 0){
                    rst = Db.use().count(Entity.create("book").set("class_", cid));
                } else {
                    rst = Db.use().count(Entity.create("book"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rst;
    }

    @Override
    public Result search(Integer curPage, Integer pageSize, Integer class_) {
        List<Book> page = null;
        try {
            // 查询指定图书类别的数据并进行分页处理
            page = Db.use().page("select * from book where class_=?", new Page(curPage - 1, pageSize), BeanListHandler.create(Book.class), class_);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultUtil.success(page);
    }

    @Override
    public Result addBook(Book book) {
        int rst = 0;
        try {
            // 执行图书添加
            rst = Db.use().insert(Entity.create("book").parseBean(book, true, true));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(rst ==  1){
            // 如果添加成功
            return ResultUtil.success(rst);
        }
        return ResultUtil.failure();
    }

    @Override
    public Result bookListAll()  {
        List<Book> book = null;
        try {
            // 执行 图书列表查询并转化为 List<Book> 对象
            book = Db.use().findAll(Entity.create("book"), Book.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(book == null){
            return ResultUtil.failure();
        }
        return ResultUtil.success(book);
    }

    @Override
    public Result updateBook(Book book) {
        int rst = 0;
        try {
            // 这里直接用Entity类提供的parseBean快捷的将实体类对象转换为用与更新的set条件语句，然后根据图书id进行更新
            rst = Db.use().update(Entity.create().parseBean(book, true, true)
            ,Entity.create("book").set("book_id",book.getBookId()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(rst == 1){
            return ResultUtil.success(rst);
        }
        return ResultUtil.failure();
    }

    @Override
    public Result delBook(String bookId) {
        int rst = 0;
        try {
            rst = Db.use().del(Entity.create("book").set("book_id", bookId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(rst == 0){
            // 删除失败
            return ResultUtil.failure();
        }
        return ResultUtil.success(rst);
    }


}
